package com.example.myapplication.xiaochengxu;

import com.example.myapplication.shortvideo.utils.GsonUtil;
import com.example.myapplication.shortvideo.utils.TextUtils;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * @author: rongfeng
 * @date: 2020/12/10
 * @description
 */
public class DowdLoadAudio {
    List<maoer.Bean> urlList = new ArrayList<>();
    volatile int mIndex = -1;
    ExecutorService pool;

    public void addList(List<maoer.Bean> list) {
        urlList = list;
        start();
    }


    public void start() {
        pool = Executors.newFixedThreadPool(1);
        for (int i = 0; i < 1; i++) {
            next();
        }
    }

    public void next() {
        pool.execute(new Runnable() {
            @Override
            public void run() {
                mIndex++;
                if (mIndex < urlList.size()) {
                    getAutioUrl(urlList.get(mIndex));
                }
            }
        });
    }

    public void getAutioUrl(maoer.Bean data) {
        String url = "https://www.missevan.com/sound/getsound?soundid=" + data.sound_id;
        OkHttpClient okHttpClient = new OkHttpClient();
        final Request request = new Request.Builder()
                .url(url)
                .get()//默认就是GET请求，可以不写
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String str = response.body().string();
                maoer.Bean.sound bean = GsonUtil.parse(str, maoer.Bean.sound.class);
                String path = bean.info.sound.soundurl_128;
                if (path == null || path.equals("")) {
                    path =  bean.info.sound.soundurl;
                }
                data.url = path;
                download(data);
            }
        });
    }


    public void download(maoer.Bean data) {
        // 下载网络文件
        int bytesum = 0;
        int byteread = 0;

        URL url = null;
        try {
//            String path = data.playPathHq;
//            if (path == null || path.equals("")) {
//                path = data.playPathAacv164;
//            }
//            if (path == null || path.equals("")) {
//                path = data.playPathAacv224;
//            }
            url = new URL(data.url);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        InputStream inStream = null;
        FileOutputStream fs = null;
        try {
            URLConnection conn = url.openConnection();
            inStream = conn.getInputStream();
            fs = new FileOutputStream("D:\\wrf\\test\\audio2\\" + data.id + ".m4a");
            byte[] buffer = new byte[1024];
            int length;
            while ((byteread = inStream.read(buffer)) != -1) {
                bytesum += byteread;
                fs.write(buffer, 0, byteread);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inStream != null) {
                try {
                    inStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fs != null) {
                try {
                    fs.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        next();
        System.out.println("ssss " + mIndex);
    }


}
